<?php

namespace app\admin\controller;

use app\common\Helper;
use app\admin\model\Advertiselocal as AdvertiselocalMdl;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

/**
 * 广告位
 */
class Advertiselocal extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new AdvertiselocalMdl();
    }

    /**
     * 列表
     */
    public function getList(){
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'status' => 1
        ];
        $model = $this->model;
        $order = 'createDate DESC';
        $result = $model->where($where)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();

        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关广告位');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 添加/编辑 保存
     */
    public function save(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->advertiseLocalName)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'advertiseLocalName 不能为空');
        }
        if (empty($param->type) && $param->type != 0) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 不能为空');
        }
        if (!in_array($param->type, [0, 1])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 格式不正确');
        }
        if (empty($param->advertiseForCharge) && $param->advertiseForCharge != 0) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'advertiseForCharge 不能为空');
        }
        if (empty($param->topimgurl)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'topimgurl 不能为空');
        }

        Db::startTrans();
        $data = (array)$param;
        $maxNum = $model->where('type', $param->type)->max('advertiseLocalNumb');
        $maxNum = $maxNum ? $maxNum : 0;
        $data['advertiseLocalNumb'] = $maxNum + 1;
        $data['advertiseForCharge'] = $data['advertiseForCharge'] * 100;
        if(isset($param->imgurls) && $param->imgurls) {
            $data['imgurl'] = implode(',', $param->imgurls);
        }
        if (!empty($param->id)) {
            // 修改
            $id = $param->id;
            $result = $model->allowField(true)->save($data, ['id' => $id]);

            // 图片
            $res = true;
            if(isset($param->imgurls) && $param->imgurls) {
                $imgurls = (array) $param->imgurls;
                foreach($imgurls as $key => $imgurl){
                    $res = Image::where(['imgUrl' => $imgurl])->update(['relatedId' => $data['id'], 'controller' => 'Advertiselocal', 'relatedTable' => 'advertiselocal', 'tag' => 'imglist', 'sorting' => $key]);
                }
            }
            if(!$res){
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '图片保存失败');
            }

            // Query执行后的操作
            $model->_after_update($data);
        } else {
            // 新增
            $data['id'] = Helper::getUUID();
            $data['createDate'] = date('Y-m-d H:i:s');
            $result = $model->save($data);

            // 图片
            $res = true;
            if(isset($param->imgurls) && $param->imgurls) {
                $imgurls = (array) $param->imgurls;
                foreach($imgurls as $key => $imgurl){
                    $res = Image::where(['imgUrl' => $imgurl])->update(['relatedId' => $data['id'], 'controller' => 'Advertiselocal', 'relatedTable' => 'advertiselocal', 'tag' => 'imglist', 'sorting' => $key]);
                }
            }
            if(!$res){
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '图片保存失败');
            }

            // Query执行后的操作
            $model->_after_insert($data);
        }

        if ($result !== false) {
            Db::commit();
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '保存成功');
        } else {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能为空');
        }

        $id = $param->id;
        Db::startTrans();
        try {
            $result = $model->where('id', $id)->update(['status' => 2]);
            if($result !== false){
                // Query执行后的操作
                $model->_after_delete($id);

                Db::commit();
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '删除成功');
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $e->getMessage());
        }
    }

}